package org.loofer.uiframework.navigator;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import org.loofer.ext.component.logger.Logger;
import org.loofer.ext.utils.StringUtils;
import org.loofer.uiframework.navigator.backstack.AbstractFragment;
import org.loofer.uiframework.navigator.backstack.IFragmentBackHelper;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public final class Navigator {
    private static final String TAG = "Navigator";
    private static final Map<String, NavigatorOptions> mNavigatorOptionsHashMap = new HashMap();
    private static final Map<String, NavigatorParams> mNavigatorParamsHashMap = new HashMap();
    private static final Map<String, NavigatorOptions> mSchemeNavigatorOptionsHashMap = new HashMap();
    private static FragmentBackHelperFactory sFragmentBackHelperFactory;
    private static Context sGlobalContext;
    private Bundle mBundle;
    private int mContainerId;
    private int mEnterAnimRes;
    private AbstractFragment mFragment;
    private IFragmentBackHelper mFragmentIFragmentBackHelper;
    private int mLaunchFlag;
    private NavigatorCloser mNavigatorCloser;
    private int mPopExitAnimRes;
    private int mRequestCode;
    private String mUrl;

    public static final class Builder {
        private Bundle mBundle;
        private int mContainerId;
        private int mEnterAnimRes;
        private AbstractFragment mFragment;
        private IFragmentBackHelper mFragmentBackHelper;
        private int mLaunchFlag = 268435456;
        private NavigatorCloser mNavigatorCloser;
        private int mPopExitAnimRes;
        private int mRequestCode = -1;
        private Uri.Builder mUriBuilder;
        private String mUrl;

        private void checkPageIsSet() {
            if (this.mUriBuilder == null) {
                throw new IllegalAccessError("should call setPageName before you call addParameter");
            }
        }

        public Builder addParameter(@NonNull String key, @NonNull String value) {
            checkPageIsSet();
            this.mUriBuilder.appendQueryParameter(key, value);
            return this;
        }

        public Builder addParameter(@NonNull String key, @NonNull Number value) {
            checkPageIsSet();
            this.mUriBuilder.appendQueryParameter(key, value.toString());
            return this;
        }

        public Builder addParameter(@NonNull String key, @NonNull Boolean value) {
            checkPageIsSet();
            this.mUriBuilder.appendQueryParameter(key, value.toString());
            return this;
        }

        public Builder setPageName(@NonNull String pageName) {
            this.mUriBuilder = Uri.parse(NavigatorConfig.instance().getAppSchema() + "://page/" + pageName).buildUpon();
            return this;
        }

        public Builder setUrl(@NonNull String url) {
            this.mUrl = url;
            this.mUriBuilder = Uri.parse(url).buildUpon();
            return this;
        }

        public void setNavigatorCloser(NavigatorCloser navigatorCloser) {
            this.mNavigatorCloser = navigatorCloser;
        }

        public Builder setFragmentBackHelper(IFragmentBackHelper fragmentBackHelper) {
            this.mFragmentBackHelper = fragmentBackHelper;
            return this;
        }

        public void setContainerId(int containerId) {
            this.mContainerId = containerId;
        }

        public void setEnterAnimRes(int enterAnimRes) {
            this.mEnterAnimRes = enterAnimRes;
        }

        public void setPopExitAnimRes(int popExitAnimRes) {
            this.mPopExitAnimRes = popExitAnimRes;
        }

        public void setLaunchFlag(int launchFlag) {
            this.mLaunchFlag = launchFlag;
        }

        public Builder setRequestCode(AbstractFragment fragment, int requestCode) {
            this.mFragment = fragment;
            this.mFragment.setRequestCode(requestCode);
            this.mRequestCode = requestCode;
            return this;
        }

        public void setBundle(Bundle bundle) {
            this.mBundle = bundle;
        }

        public Navigator build() {
            if (this.mFragmentBackHelper == null && Navigator.sFragmentBackHelperFactory != null) {
                this.mFragmentBackHelper = Navigator.sFragmentBackHelperFactory.getCurrentFragmentBackHelper();
            }
            Navigator navigator = new Navigator();
            navigator.setUrl(this.mUriBuilder.build().toString());
            navigator.setLaunchFlag(this.mLaunchFlag);
            navigator.setPopExitAnimRes(this.mPopExitAnimRes);
            navigator.setEnterAnimRes(this.mEnterAnimRes);
            navigator.setContainerId(this.mContainerId);
            navigator.setFragmentIFragmentBackHelper(this.mFragmentBackHelper);
            navigator.setNavigatorCloser(this.mNavigatorCloser);
            navigator.setBundle(this.mBundle);
            navigator.setRequestCode(this.mFragment, this.mRequestCode);
            return navigator;
        }
    }

    private Navigator() {
        this.mLaunchFlag = 268435456;
        this.mRequestCode = -1;
    }

    public static void registerFragment(@NonNull String pageName, FragmentLauncher fragmentLauncher) {
        NavigatorOptions options = new NavigatorOptions();
        options.setFragmentLauncher(fragmentLauncher);
        mNavigatorOptionsHashMap.put(pageName, options);
    }

    public static void registerFragment(@NonNull String pageName, Class<? extends AbstractFragment> clazz) {
        registerFragment(pageName, clazz, null);
    }

    public static void registerActivity(@NonNull String pageName, ActivityLauncher launcher) {
        NavigatorOptions options = new NavigatorOptions();
        options.setActivityLauncher(launcher);
        mNavigatorOptionsHashMap.put(pageName, options);
    }

    public static void registerScheme(@NonNull String schemeName, Class<? extends Activity> clazz) {
        NavigatorOptions navigatorOptions = new NavigatorOptions();
        navigatorOptions.setOpenActivityClass(clazz);
        mSchemeNavigatorOptionsHashMap.put(schemeName, navigatorOptions);
    }

    public static void registerActivity(@NonNull String pageName, Class<? extends Activity> clazz) {
        registerActivity(pageName, clazz, null);
    }

    public static void registerActivity(@NonNull String pageName, Class<? extends Activity> clazz, NavigatorOptions navigatorOptions) {
        if (navigatorOptions == null) {
            navigatorOptions = new NavigatorOptions();
        }
        navigatorOptions.setOpenActivityClass(clazz);
        mNavigatorOptionsHashMap.put(pageName, navigatorOptions);
    }

    public static void init(Context context, String appSchema) {
        sGlobalContext = context;
        NavigatorConfig.instance().setAppSchema(appSchema);
    }

    public static void setFragmentBackHelperFactory(FragmentBackHelperFactory factory) {
        sFragmentBackHelperFactory = factory;
    }

    public boolean open() {
        if (!StringUtils.isEmpty(this.mUrl)) {
            return open(this.mUrl);
        }
        throw new IllegalArgumentException("the mUrl is empty. should config with Builder first");
    }

    public boolean open(String url) {
        Logger.d(TAG, "open url." + url);
        NavigatorParams params = createNavigatorParamsFromUrl(url, new NavigatorURL(url));
        Intent intent;
        if (params != null) {
            params.setBundle(this.mBundle);
            NavigatorOptions options = params.getOptions();
            if (options == null) {
                Logger.e(TAG, "options create failed, ignore " + url);
                return false;
            }
            if (options.getOpenActivityClass() != null || options.getActivityLauncher() != null) {
                if (options.getActivityLauncher() != null) {
                    options.getActivityLauncher().open(sGlobalContext, params);
                } else {
                    intent = intentFor(params);
                    intent.setClass(sGlobalContext, options.getOpenActivityClass());
                    if (this.mRequestCode >= 0) {
                        this.mFragment.startActivityForResult(intent, this.mRequestCode);
                    } else {
                        intent.addFlags(this.mLaunchFlag <= 0 ? 268435456 : this.mLaunchFlag);
                        sGlobalContext.startActivity(intent);
                    }
                }
                if (this.mNavigatorCloser != null) {
                    this.mNavigatorCloser.close(params);
                }
            } else if (!((options.getOpenFragmentClass() == null || this.mFragmentIFragmentBackHelper == null) && options.getFragmentLauncher() == null)) {
                if (options.getFragmentLauncher() != null) {
                    options.getFragmentLauncher().open(this.mFragmentIFragmentBackHelper, params);
                } else {
                    try {
                        AbstractFragment fragment = (AbstractFragment) options.getOpenFragmentClass().newInstance();
                        fragment.setArguments(bundleFor(params));
                        if (this.mContainerId <= 0 || this.mEnterAnimRes <= 0 || this.mPopExitAnimRes <= 0 || this.mLaunchFlag <= 0) {
                            this.mFragmentIFragmentBackHelper.launchFragment(fragment);
                        } else {
                            this.mFragmentIFragmentBackHelper.launchFragment(fragment, this.mContainerId, this.mEnterAnimRes, this.mPopExitAnimRes, this.mLaunchFlag);
                        }
                    } catch (InstantiationException e) {
                        Logger.e(TAG, "InstantiationException", e);
                        return false;
                    } catch (IllegalAccessException e2) {
                        Logger.e(TAG, "IllegalAccessException", e2);
                        return false;
                    }
                }
            }
            return true;
        }
        try {
            intent = new Intent("android.intent.action.VIEW");
            intent.setData(Uri.parse(url));
            intent.addFlags(this.mLaunchFlag <= 0 ? 268435456 : this.mLaunchFlag);
            sGlobalContext.startActivity(intent);
            return true;
        } catch (ActivityNotFoundException exception) {
            Logger.e(TAG, "ActivityNotFoundException, ignore " + url);
            Logger.e(TAG, exception.getMessage());
            return false;
        }
    }

    private static Intent intentFor(NavigatorParams params) {
        NavigatorOptions options = params.getOptions();
        Intent intent = new Intent();
        if (options.getDefaultParams() != null) {
            for (Entry<String, String> entry : options.getDefaultParams().entrySet()) {
                intent.putExtra((String) entry.getKey(), (String) entry.getValue());
            }
        }
        for (Entry<String, String> entry2 : params.getParams().entrySet()) {
            intent.putExtra((String) entry2.getKey(), (String) entry2.getValue());
        }
        return intent;
    }

    private static Bundle bundleFor(NavigatorParams params) {
        NavigatorOptions options = params.getOptions();
        Bundle intent = new Bundle();
        if (options.getDefaultParams() != null) {
            for (Entry<String, String> entry : options.getDefaultParams().entrySet()) {
                intent.putString((String) entry.getKey(), (String) entry.getValue());
            }
        }
        for (Entry<String, String> entry2 : params.getParams().entrySet()) {
            intent.putString((String) entry2.getKey(), (String) entry2.getValue());
        }
        return intent;
    }

    private static NavigatorParams createNavigatorParamsFromUrl(String url, NavigatorURL mNavigatorURL) {
        if (!mNavigatorURL.isValid()) {
            Logger.w(TAG, "Not valid for url " + url);
            return null;
        } else if (mNavigatorParamsHashMap.get(url) != null) {
            return (NavigatorParams) mNavigatorParamsHashMap.get(url);
        } else {
            NavigatorOptions navigatorOptions;
            NavigatorParams navigatorParams = null;
            for (Entry<String, NavigatorOptions> entry : mSchemeNavigatorOptionsHashMap.entrySet()) {
                navigatorOptions = (NavigatorOptions) entry.getValue();
                if (((String) entry.getKey()).equalsIgnoreCase(mNavigatorURL.getSchema())) {
                    navigatorParams = new NavigatorParams();
                    mNavigatorURL.getPageParamMap().put("url", url);
                    navigatorParams.setParams(mNavigatorURL.getPageParamMap());
                    navigatorParams.setOptions(navigatorOptions);
                    break;
                }
            }
            for (Entry<String, NavigatorOptions> entry2 : mNavigatorOptionsHashMap.entrySet()) {
                navigatorOptions = (NavigatorOptions) entry2.getValue();
                if (((String) entry2.getKey()).equalsIgnoreCase(mNavigatorURL.getPageName())) {
                    navigatorParams = new NavigatorParams();
                    navigatorParams.setParams(mNavigatorURL.getPageParamMap());
                    navigatorParams.setOptions(navigatorOptions);
                    break;
                }
            }
            if (navigatorParams == null) {
                Logger.w(TAG, "No route found for url " + url);
                return null;
            }
            mNavigatorParamsHashMap.put(url, navigatorParams);
            return navigatorParams;
        }
    }

    public static void registerFragment(String pageName, Class<? extends AbstractFragment> clazz, NavigatorOptions options) {
        if (options == null) {
            options = new NavigatorOptions();
        }
        options.setOpenFragmentClass(clazz);
        mNavigatorOptionsHashMap.put(pageName, options);
    }

    public void setLaunchFlag(int launchFlag) {
        this.mLaunchFlag = launchFlag;
    }

    public void setRequestCode(AbstractFragment fragment, int requestCode) {
        this.mRequestCode = requestCode;
        this.mFragment = fragment;
    }

    public void setNavigatorCloser(NavigatorCloser navigatorCloser) {
        this.mNavigatorCloser = navigatorCloser;
    }

    public void setFragmentIFragmentBackHelper(IFragmentBackHelper fragmentIFragmentBackHelper) {
        this.mFragmentIFragmentBackHelper = fragmentIFragmentBackHelper;
    }

    public void setContainerId(int containerId) {
        this.mContainerId = containerId;
    }

    public void setEnterAnimRes(int enterAnimRes) {
        this.mEnterAnimRes = enterAnimRes;
    }

    public void setPopExitAnimRes(int popExitAnimRes) {
        this.mPopExitAnimRes = popExitAnimRes;
    }

    public void setUrl(String url) {
        this.mUrl = url;
    }

    public void setBundle(Bundle bundle) {
        this.mBundle = bundle;
    }
}
